package com.example.lenovo.newsdemo;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;


import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final int GET_NEWS_MESSAGE = 1001;
    private Toolbar toolbar;
    private TabLayout news_tabLayout;
    private ViewPager news_viewPager;
    private List<android.support.v4.app.Fragment> list_Fragment = new ArrayList<>();
    private List<String> list_Title = new ArrayList<>();

    private Handler handler;


    private String topUrl = "http://v.juhe.cn/toutiao/index?type=top&key=f384866c61f5dcd3588f713a1597d583";

    private RecyclerView recyclerView;
    private NewsData newsData;
    private MyAdapter myAdapter = new MyAdapter(MainActivity.this,newsData);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //initTitile();
        //initFragment();
        getDataFromApi();
        initHandler();

     //   news_viewPager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager(),list_Fragment,list_Title));
     //   news_tabLayout.setupWithViewPager(news_viewPager);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);





    }



    private void initHandler() {
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if(message.what == GET_NEWS_MESSAGE){
                     newsData = (NewsData) message.obj;
                    Log.e("data",newsData.getResult().getData().get(0).getDate());

                    myAdapter = new MyAdapter(MainActivity.this,newsData);
                    myAdapter.notifyDataSetChanged();
                    recyclerView.setAdapter(myAdapter);

                    myAdapter.setmOnItemClickListener(new MyAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            NewsData.ResultBean.DataBean dataBean = newsData.getResult().getData().get(position);
                            String url = dataBean.getUrl();
                            Intent intent = new Intent(MainActivity.this,webViewActivity.class);
                            intent.putExtra("URL",url);
                            startActivityForResult(intent,1001);
                        }
                    });

                }

                return false;
            }
        });
    }

    private void getDataFromApi() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(topUrl)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("MainActivity","请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String newsDataJsonStr = response.body().string();
                 newsData = new Gson().fromJson(newsDataJsonStr,NewsData.class);
               // Log.e("data",newsData.getResult().getData().get(0).getAuthor_name());
                Message message = Message.obtain();
                message.what = GET_NEWS_MESSAGE;
                message.obj = newsData;
                handler.sendMessage(message);

            }
        });
    }

    private void initFragment() {
        newsFragment n = new newsFragment();
        list_Fragment.add(n);

    }

    private void initTitile() {
        list_Title.add("头条");
      //  list_Title.add("新闻");
       // list_Title.add("体育");
        news_tabLayout.setTabMode(TabLayout.MODE_FIXED);
        //Log.e("list",list_Title.get(0));
        news_tabLayout.addTab(news_tabLayout.newTab().setText(list_Title.get(0)));
       // news_tabLayout.addTab(news_tabLayout.newTab().setText(list_Title.get(1)));
      //  news_tabLayout.addTab(news_tabLayout.newTab().setText(list_Title.get(2)));

    }


    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("news");
      //  news_tabLayout = findViewById(R.id.news_tabLayout);
      //  news_viewPager = findViewById(R.id.news_viewPager);
        recyclerView = findViewById(R.id.recylerView);
    }
}
